<?php
/*
 * @author:qiushaochang
 * @date:2023/8/11
*/

namespace crmeb\services\pay\extend\quicknpay;

use crmeb\exceptions\ApiException;
use crmeb\services\HttpService;
use crmeb\services\pay\BasePay;
use crmeb\services\pay\PayInterface;
use GuzzleHttp\Client;
use think\facade\Config;
use think\helper\Arr;

/**
 *
 */
class QuicknPay
{
    /**
     * @var mixed
     */
    private $apiKey;
    /**
     * @var mixed
     */
    private $merchantId;
    /**
     * @var mixed
     */
    private $billerId;
    /**
     * @var array|\ArrayAccess|mixed
     */
    private $apiEnv;
    /**
     * @var string[]
     */
    private $apiGroups = [
        'sandbox' => [
            'direct_pay' => 'https://www.demo.betterpay.me/merchant/api/v2/lite/direct/receiver',
            'channels' => 'https://www.demo.betterpay.me/merchant/api/v2/lite/channels',
        ],
        'pro' => [
            'direct_pay' => 'https://lite.betterpay.me/api/merchant/v1/direct',
            'channels' => 'https://lite.betterpay.me/api/merchant/v1/channels',
        ]
    ];

    /**
     * QuicknPay constructor.
     * @param array $config
     */
    public function __construct(array $config)
    {
        $config           = Config::get('pay.stores.quickn_pay');
        $this->apiEnv     = Arr::get($config, 'api_env');
        $this->apiKey     = Arr::get($config, 'api_key');
        $this->merchantId = Arr::get($config, 'merchant_id');
        $this->billerId   = Arr::get($config, 'biller_id');
    }

    /**
     * @param $type
     * @return string
     */
    private function getApiByType($type)
    {
        $apiGroup = Arr::get($this->apiGroups, $this->apiEnv);
        if (!Arr::get($apiGroup, $type)) {
            throw new ApiException("未知接口");
        }
        return $apiGroup[$type];
    }

    /**
     * @param $data
     * @return string
     */
    public function sign($data)
    {
        ksort($data);
        $hash_string = implode("", $data);
        $hash_string = str_replace(' ', '', $hash_string);
        return hash_hmac('sha256', $hash_string, $this->apiKey);
    }

    /**
     * @param $url
     * @param $data
     * @return string
     */
    private function postRequest($url, $data)
    {
        $data['merchant_id'] = $this->merchantId;
        $hash                = $this->sign($data);
        $data['hash']        = $hash;
        $client              = new Client(['verify' => false]);
        $result              = $client->request("post", $url, [
            'json' => $data,
        ]);
        return $result->getBody()->getContents();
    }

    /**
     * @param $url
     * @param $data
     * @return string
     */
    private function getRequest($url, $data)
    {
        $data['merchant_id'] = $this->merchantId;
        $hash                = $this->sign($data);
        $data['hash']        = $hash;
        $res                 = HttpService::postRequest($url, $data, ['Content-Type:application/json']);
        return $res;
    }

    public function request($data)
    {

    }

    /**
     * @param $data
     * @return string
     */
    public function directPay($data)
    {
        $url = $this->getApiByType('direct_pay');
        $res = $this->postRequest($url, $data);
        return json_decode($res, true);
    }

    /**支付渠道
     * @param $data
     * @return string
     */
    public function getChannels($data)
    {
        $url = $this->getApiByType('channels');
        $res = $this->postRequest($url, $data);
        return json_decode($res, true);
    }
}